package com.medaxis.hpams.sso.controller;


import com.medaxis.hpams.dto.global.BaseResponse;
import com.medaxis.hpams.dto.global.ErrorException;
import com.medaxis.hpams.dto.global.SuccessResponse;
import com.medaxis.hpams.sso.constant.Constants;
import com.medaxis.hpams.sso.domain.DictType;
import com.medaxis.hpams.sso.domain.dto.DictTypeDto;
import com.medaxis.hpams.sso.service.DictTypeService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping(Constants.VERSION_NUMBER+"/sso/dict/type")
@Api(tags = Constants.VERSION_NUMBER+"字典类型管理")
public class DictTypeController extends BaseController {

    @Autowired
    private  DictTypeService dictTypeService;

    @GetMapping("/list")
    @ApiOperation(value = "获取字典类型列表",httpMethod = "GET")
    public BaseResponse list(DictTypeDto dictTypeDto) {
        startPage();
        List<DictTypeDto> list= dictTypeService.selectDictTypeList(dictTypeDto);
        return SuccessResponse.success(getDataTable(list));
    }

    /**
     * 查询字典类型详细
     */
    @ApiOperation(value = "查询字典详情",httpMethod = "GET")
    @GetMapping(value = "/{id}")
    public BaseResponse<DictType> getInfo(@PathVariable Long id) {
        return new SuccessResponse(dictTypeService.selectDictTypeById(id));
    }
////
//
    /**
     * 新增字典类型
     */
    @ApiOperation(value = "新增字典类型",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "systemId", value = "系统归属", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "字典类型状态", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "dictType", value = "字典类型", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "dictName", value = "字典类型名称", required = true, dataType = "String", paramType = "query"),
    })
    @PostMapping("/add")
    public BaseResponse add(@Validated DictTypeDto  dictDto) {
        if (Constants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dictDto))) {
            throw new ErrorException("新增字典'" + dictDto.getDictName() + "'失败，字典类型已存在");
        }
        //todo 新增创建人
        dictDto.setCreateBy("admin");
        return  toBaseResponse(dictTypeService.insertDictType(dictDto));
    }

    /**
     * 修改字典类型
     */
    @ApiOperation(value = "修改字典类型",httpMethod = "PUT")
    @PutMapping("/edit")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "systemId", value = "系统归属", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "字典类型状态", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "dictType", value = "字典类型", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "dictName", value = "字典类型名称", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "字典ID", required = true, dataType = "String", paramType = "query"),
    })
    public  BaseResponse edit(@Validated  DictTypeDto dictDto) {
        if (Constants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dictDto))) {
             throw new ErrorException("修改字典'" + dictDto.getDictName() + "'失败，字典类型已存在");
        }
        //todo 新增创建人
//        dict.setUpdateBy(SecurityUtils.getUsername());
        dictDto.setUpdateBy("admin");
        return  toBaseResponse(dictTypeService.updateDictType(dictDto));

    }

    /**
     * 删除字典类型
     */
    @ApiOperation(value = "删除字典类型",httpMethod = "DELETE")
    @ApiImplicitParam(name = "ids", value = "用户ID集合", required = true, paramType="path")
    @DeleteMapping("/{ids}")
    public BaseResponse remove(@PathVariable Long[] ids) {
        return  toBaseResponse(dictTypeService.deleteDictTypeByIds(ids));
    }



}
